<?php
namespace App\Libraries\Common;

use Config;
use Log;
use \Curl\Curl;
use Illuminate\Support\Facades\Redis;

class Sms{
	CONST BASE_URL = 'https://sms.yunpian.com';
	CONST URI_SEND = '/v1/sms/send.json';
	CONST VERIFYCODE = 'ft:vfcode';
	CONST EXPIRE = 1800;
	CONST SEND_LIMIT_TIME = 60;
	CONST MAX_DAILY_SEND = 20;
	private $apiKey = '';
	private $smsSign = '';
	function __construct(){
		$this->apiKey = Config::get('sms.api_key');
		$this->smsSign = Config::get('sms.sign');
	}

  /**
   * 
   * @param string $mobile
   * @return {status,message} status:ok=0
   */
	public function sendVerifyCode($mobile){
		$code = $this->genVerifyCode($mobile);
		$ret = ['status'	=>	'0','message' => 'OK'];
		if($code == -1 || $code == -2){
			$ret['status'] = -1;
			$ret['message'] = '发送过于频繁';
			return $ret;
		}
		$reqUrl = self::BASE_URL . self::URI_SEND;
		if(strlen($mobile) != 11 && substr($mobile, 0, 3) != '+86'){
			$smsContent = "【Futuretour】Hello,your verify code is ". $code;
		}
		else{
			$smsContent = "【职派咨询】您的验证码是".$code;
		}
		if(!empty($this->smsSign)){
			$smsContent = $this->smsSign . $smsContent;
		}
		$smsData = array(
			'apikey'	=>	$this->apiKey,
			'mobile'	=>	$mobile,
			'text'	=>	$smsContent,
		);
		$curl = new Curl();
		$data = $curl->post($reqUrl,$smsData);
		Log::info("send_reg_code:\t".json_encode($smsData).json_encode($data,JSON_UNESCAPED_UNICODE));
		if($data->code == 0){
			return $ret;
		}
		else{
			$ret['status'] = -2;
			$ret['message']	=	'发送失败';
			return $ret;
		}
	}

	public function sendSms($mobile,$smsContent){
		$ret = ['status'	=>	'0','message' => 'OK'];
		$reqUrl = self::BASE_URL . self::URI_SEND;
		if(!empty($this->smsSign)){
			$smsContent = $this->smsSign . $smsContent;
		}
		$smsData = array(
			'apikey'	=>	$this->apiKey,
			'mobile'	=>	$mobile,
			'text'	=>	$smsContent,
		);
		$curl = new Curl();
		$data = $curl->post($reqUrl,$smsData);
		Log::info("send_sms:\t".json_encode($smsData).json_encode($data,JSON_UNESCAPED_UNICODE));
		if($data->code == 0){
			return $ret;
		}
		else{
			$ret['status'] = -2;
			$ret['message']	=	'发送失败';
			return $ret;
		}
	}

	// public function checkSendLimit($mobile){

	// }

	public function genVerifyCode($mobile){
		$code = rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);
		$redis = Redis::connection();
		$existVerfiyData = $redis->hGet(self::VERIFYCODE,$mobile);
		$sendCnt = 0;
		if(!empty($existVerfiyData)){
			$existVerfiyData = json_decode($existVerfiyData,true);
			$lastSendTime = $existVerfiyData['send_time'];
			$sendCnt = $existVerfiyData['send_cnt'];
			if(time() - $lastSendTime < self::SEND_LIMIT_TIME){
				return -1;
			}
			if($sendCnt == self::MAX_DAILY_SEND){
				return -2;
			}
		}
		$verifyData = [
			'code'	=>	$code,
			'expire'	=>	time() + self::EXPIRE,
			'send_time'	=>	time(),
			'send_cnt'	=>	$sendCnt++,
		];
		$redis->hSet(self::VERIFYCODE,$mobile,json_encode($verifyData));
		return $code;
	}

  /**
   * 
   * @param string $mobile
   * @param string $code
   * @return boolean
   */
	public function checkVerifyCode($mobile,$code){
		$redis = Redis::connection();
		$verifyData = $redis->hGet(self::VERIFYCODE,$mobile);
		if(empty($verifyData)){
			return false;
		}
		$verifyData = json_decode($verifyData,true);
		if($verifyData['code'] == $code && time() < $verifyData['expire']){
			return true;
		}
		else{
			return false;
		}
	}

}
